package com.example.demo.http.middlewares;

import com.example.demo.services.system.ResultService;
import com.example.demo.services.system.UserService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;


public class AuthenticateMiddleware implements HandlerInterceptor {
    private Class clazz;

    public AuthenticateMiddleware(Class clazz) {
        this.clazz = clazz;
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (UserService.getThreadInstance().getLoginUser(clazz) == null) {
            //如果获取用户是空，说明没有登陆，返回false拦截，并返回json
            return ResultService.middlewareGetNotAuthResultJSON(response);
        }
        return HandlerInterceptor.super.preHandle(request, response, handler);
    }
}
